﻿// Copyright (C) 2010-2012, MaGUI
//
//  Elena Ballesteros Molinero
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Drawing;

namespace Soporte
{
    public class Keyboard
    {
        private Keyboard _instance;
        public Keyboard Instance
        {
            get
            {
                if (_instance == null) _instance = new Keyboard();
                return _instance;
            }
        }

        public bool IsShiftPressed()
        {
            return ((NativeMethods.GetKeyState(NativeMethods.VK_SHIFT) & 0x80) == 0x80 ? true : false);
        }

        public Rectangle GetActiveFormBounds()
        {
            var info = new NativeMethods.WINDOWINFO();
            NativeMethods.GetWindowInfo(NativeMethods.GetForegroundWindow(), ref info);

            var posX = info.rcWindow.Left;
            var posY = info.rcWindow.Top;
            var ancho = info.rcWindow.Right - info.rcWindow.Left;
            var alto = info.rcWindow.Bottom - info.rcWindow.Top;

            return new Rectangle(posX, posY, ancho, alto);
        }

        public Point GetCaretPos()
        {
            uint hilo = NativeMethods.GetWindowThreadProcessId(NativeMethods.GetForegroundWindow(), IntPtr.Zero);
            NativeMethods.GUITHREADINFO guiThreadInfo = new NativeMethods.GUITHREADINFO();
            guiThreadInfo.cbSize = NativeMethods.GUITHREADINFO.GetSize();

            if (NativeMethods.GetGUIThreadInfo(hilo, out guiThreadInfo))
            {
                Point pt = new Point(guiThreadInfo.rcCaret.Left, guiThreadInfo.rcCaret.Top);
                NativeMethods.ClientToScreen(guiThreadInfo.hwndCaret, out pt);
                return pt;
            }
            return Point.Empty;
        }

    }
}
